﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Text;
using System.Collections;

namespace FOBVT.Web.DBUtility
{
	/// <summary>
	/// 用于删除数据记录的方法类
	/// </summary>
	internal class DeleteMethod : BaseMethod
	{
		private static Hashtable DeleteClauseStore = new Hashtable();
		public DeleteMethod(Type infoType)
			: base(infoType)
		{
		}
		public DeleteMethod(BaseInfo info)
			: base(info)
		{
		}
		public DeleteMethod(string TableName, BaseInfo info)
			: base(TableName, info)
		{
		}

		/// <summary>
		/// 删除表所有记录行数（慎重）
		/// </summary>
		/// <returns></returns>
		public bool DeleteAll()
		{
			return Delete(null);
		}

		/// <summary>
		/// 删除指定记录
		/// </summary>
		/// <param name="WhereClause">查询条件</param>
		/// <returns></returns>
		public bool Delete(string WhereClause)
		{
			StringBuilder sb_sql = new StringBuilder();
			sb_sql.Append(this.DeleteClause);
			if (!string.IsNullOrEmpty(WhereClause))
			{
				sb_sql.AppendFormat(" WHERE {0} ", WhereClause);
			}
			sb_sql.Append(';');
			bool result = base.ExecuteBool(sb_sql.ToString(), null);
			return result;
		}
		/// <summary>
		/// 通过自增列删除，首先保证实体已存在自增列已存在并已赋值
		/// </summary>
		/// <returns></returns>
		public bool DeleteByIdentity()
		{
			if (!((base.Entity != null) && base.HasIdentity))
			{
				return false;
			}
			return this.Delete(base.IdentityWhereClause);
		}
		/// <summary>
		/// 传递自增列值进行删除
		/// </summary>
		/// <param name="Identity"></param>
		/// <returns></returns>
		//public bool DeleteByIdentity(long Identity)
		//{
		//    if (base.InfoType == null)
		//    {
		//        return false;
		//    }
		//    base.Identity = Identity;
		//    return this.DeleteByIdentity();
		//}

		/// <summary>
		/// 删除记录前部delete from ..
		/// </summary>
		public string DeleteClause
		{
			get
			{
				string result = DeleteClauseStore[base.TableName] as string;
				if (result == null)
				{
					DeleteClauseStore[base.TableName] = result = string.Format("DELETE FROM [{0}]", base.TableName);
				}
				return result;
			}
		}
	}
}